# -*- coding: utf-8 -*-

"""
Time    :2021/6/15 下午9:00
Author  :chenyang
Email   :chenyang.it@outlook.com
FileName: main.py
Software: PyCharm
"""
from flask import Blueprint, render_template, request
from app.models import Devices

main = Blueprint('main', __name__)


@main.route('/test')
def test():
    devices = Devices.query.all()
    return render_template('test.html', data=devices)


@main.route('/')
def index():
    devices = Devices.query.all()
    return render_template('index.html', data=devices)


@main.route('/search', methods=['POST'])
def search():
    search_field = request.form.get("search_field")
    search_value = request.form.get("search_value")
    if not search_value:
        devices = Devices.query.all()
        return render_template('index.html', data=devices)
    if search_field == 'sid':
        devices = Devices.query.filter(Devices.sid.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'model':
        devices = Devices.query.filter(Devices.model.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'cpu_model':
        devices = Devices.query.filter(Devices.cpu_model.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'cpu_cores':
        devices = Devices.query.filter(Devices.cpu_cores.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'bmc_address':
        devices = Devices.query.filter(Devices.bmc_address.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'purpose':
        devices = Devices.query.filter(Devices.purpose.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
    if search_field == 'owner':
        devices = Devices.query.filter(Devices.owner.like('%{0}%'.format(search_value))).all()
        return render_template('index.html', data=devices)
